
<script>
    //工具栏绑定事件
    function builderGridToolbarEvent(resData, treeGrid,myTable,layer){
       //展开|关闭全部
        $("body").on("click", "#open-close-tree", function () {
            var treedata = treeGrid.getDataTreeList("myTable");
            treeGrid.treeOpenAll("myTable", !treedata[0][treeGrid.config.cols.isOpen]);
        });
      
        $("body").on("click", "#update-cache", function () {
             //开启限制频次- start
             var requestTime = 3000;
            var lastRequestTime = store.get('update_cache_last_request_time')
             if (lastRequestTime > (new Date().getTime())) {
                    layer.msg(parseInt(requestTime / 1000) + "秒内只允许更新一次路由，无须重复更新", function () { });
                    return false;
             }
             store.set('update_cache_last_request_time', new Date().getTime() + requestTime);
             //开启限制频次- end
            var http = new Request({token_url:api.token_url,async:false,token_store_name:'rule_cache'});

            layer.confirm('<b>普通更新</b>：将当前的路由，生成路由缓存，不会修改数据;<br/><b>强制更新</b>：修改所有的路由名称，并生成新路由缓存。 ', {
                    title: '是否更新缓存路由？',
                    area: ['300px', 'auto'],
                    btn: ['普通更新', '强制更新'], //可以无限个按钮
                    skin: '', //样式类名
                }, function (index, layero) {
                    var res = http.request('post',resData.top_update_cache_url);
                    if(res.code == 0){
                        layer.msg(res.msg);
                    }else{
                        layer.msg(res.msg,function(){});
                    }
                }, function (index) {
                    var res = http.request('post',resData.top_update_cache_url,{'is_update':true});
                    if(res.code == 0){
                        layer.msg(res.msg);
                    }else{
                        layer.msg(res.msg,function(){});
                    }
                });
           
        });

        //绑定添加事件
        $("body").on("click", "#tool_add", function () {
             
            //二维码显示
            if (resData.top_add_show_qrcode) {
                var html = '<style>#top_add_qrcode{align-items: center;display:flex;justify-content:center;height:100%;}#top_add_qrcode img{width:160px;height:160px;}<\/style><div id="top_add_qrcode"></div><script>new QRCode("top_add_qrcode", { text:"' + resData["top_add_url"] + '",width: "160",height: "160",colorDark: "#000000",colorLight: "#ffffff",correctLevel: QRCode.CorrectLevel.H});<\/script>';
                layer.open({
                    type: 1,
                    title: '请扫一扫二维码',
                    area: ['250px', '250px'], //宽高
                    skin: 'layui-layer-demo', //样式类名
                    closeBtn: 1, //不显示关闭按钮
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    content: html
                });

            } else {
             
                //顶部添加事件，删除?后面的参数
                window.location.href = "//" + window.location.host + window.location.pathname + "#" + resData['top_add_url'];
                   //跳转到添加页面 通过修改hash实现跳转
                // location.hash = resData['top_add_url'];
                // window.open(resData['top_add_url'], "_self");
            }
        });
        //绑定删除事件
        $("body").on("click", "#tool_delete", function () {
            var checkStatus = treeGrid.checkStatus("myTable");
          
            var ids = '';
            if (checkStatus.data.length > 0) {
                for (var i = 0; i < checkStatus.data.length; i++) {
                    ids += checkStatus.data[i]['id'] + ",";
                }
                ids = ids.substr(0, ids.length - 1);
            }
             if (ids != '') {
                layer.confirm('ID组：' + ids, {
                    title: '是否删除以下数据？',
                    area: ['300px', 'auto'],
                    btn: ['确定', '取消'], //可以无限个按钮
                    skin: '', //样式类名
                }, function (index, layero) {
                    //删除所有的ids
                    var http = new Request({ token_url: api.token_url, async: false, token_store_name: 'delete' });
                    var res = http.request('post', resData.top_delete_url, { ids: ids });
                    if (res.code == 0) {
                        layer.msg('删除成功');
                        myTable.reload();
                    } else {
                        layer.msg('删除失败！', function () { });
                    }

                }, function (index) {
                    //按钮【按钮二】的回调
                    layer.closeAll();
                });


            } else {
                layer.msg('请勾选需要删除的数据！', function () { });
            }
        });

       
    }
    
</script>